using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Scenario_Editor.Tools
{
    internal class TextureSorter
    {
        #region Private Fields

        private int[] newTextureIDs;
        private string[] texturenames;

        #endregion Private Fields

        #region Public Constructors

        public TextureSorter(string[] currentTextureArray, string[] oldTextureArray, bool returnNegativeIDs)
        {
            // Make all names to upper ones
            for (int i = 0; i < currentTextureArray.Length; i++)
            {
                currentTextureArray[i] = currentTextureArray[i].ToUpper();
            }
            for (int i = 0; i < oldTextureArray.Length; i++)
            {
                oldTextureArray[i] = oldTextureArray[i].ToUpper();
            }

            newTextureIDs = new int[oldTextureArray.Length];
            if (returnNegativeIDs)
            {
                for (int i = 0; i < newTextureIDs.Length; i++)
                {
                    newTextureIDs[i] = -1;
                }
            }

            texturenames = new string[oldTextureArray.Length];
            List<string> missingTextures = new List<string>();

            for (int i = 0; i < oldTextureArray.Length; i++)
            {
                for (int id = 0; id < currentTextureArray.Length; id++)
                {
                    if (currentTextureArray[id] == oldTextureArray[i])
                    {
                        newTextureIDs[i] = id;
                        break;
                    }
                    if (currentTextureArray.Length - 1 == id)//If Texture is not found, open a window to select a new Texture or ...
                    {
                        missingTextures.Add(oldTextureArray[i] + "\n");
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < missingTextures.Count; i++)
            {
                sb.Append(missingTextures[i]);
            }
            if (missingTextures.Count > 0)
            {
                MessageBox.Show("Missing Textures:\n ===================\n" + sb.ToString());
            }
        }

        #endregion Public Constructors

        #region Public Methods

        public int ReturnCurrentID(int oldTextureID)
        {
            if (newTextureIDs[oldTextureID] == -1)
                return 0;
            return newTextureIDs[oldTextureID];
        }

        #endregion Public Methods
    }
}